#!/bin/sh
CONFIGFILE="./TestSuite.config"

function printHelp {
        source $CONFIGFILE
	COMPONENTS="`ls -F $TestSuiteLocation/software_tests | grep '/' | sed 's/\///'`"
        usage='Usage: ./start_tests [-C component1,component2] [-t tags] [-c config_file]
TAG can be any of the following or in combinations as described in pybot --help for the 'include' option:
---------------------------------------------------------------------------------------------------------';
        echo "$usage";
	for i in $COMPONENTS; do
		echo "Tags for $i component:"
		echo "==============================="
		tags=$(find $TestSuiteLocation/software_tests/$i -name '*tsv' -exec grep '\[Tags\]' {} \; | cut -f 3 | sed 's/.*\"\(.*\)\".*/	\1/' | sort | uniq)
		echo "$tags";
		echo ""
	done	
}

while getopts "t:c:C:h" options; do
  case $options in
    t ) TESTTYPE=$OPTARG
        ;;
    c ) CONFIGFILE=$OPTARG
        ;;
    C ) COMPONENT_NAMES=$OPTARG
        ;;
    h ) printHelp
        exit 0
        ;;
   \? ) printHelp
        exit 1
        ;;
  esac
done

source $CONFIGFILE

PYBOT_CMD="pybot -c critical"
if [ "$TS_TESTUSER_ALT" = "" ] ; then
	echo "WARN: Secondary username not set, skipping multiuser tests"
	PYBOT_CMD="$PYBOT_CMD -e multiuser"
fi

if [ "$TS_GRIDFTP_HOST2" = "" ] ; then
	echo "WARN: Secondary hostname not set, skipping thirdparty transfer tests"
	PYBOT_CMD="$PYBOT_CMD -e thirdparty"
fi

if [ "$OutputFolder" != "" ] ; then
	PYBOT_CMD="$PYBOT_CMD -d $OutputFolder"
fi

if [ "$COMPONENT_NAMES" != "" ] ; then
	COMPONENTS=$(echo $COMPONENT_NAMES | sed 's/,/ /g')
	for component in $COMPONENTS ; do
		PYBOT_CMD="$PYBOT_CMD -t Software_Tests.$component*"
	done
fi

if [ "$UseTestSuiteGSI" = "yes" ] ; then
	#export GRID_SECURITY_DIR="$GSI_CERTS/grid-security"
	#export X509_CERT_DIR="$GRID_SECURITY_DIR/certificates"
#	if [ "$GlobalTestHost" != "`hostname -f`" -a "$GlobalTestHost" != "`hostname -s`" ]; then
#		echo "You are using the GSI generated by this test suite \
#with a different host. Please make sure you have copied the necessary \
#certificateson the server side as well."
#	fi
	# Prepare testUserFolder
	USERHOME=`eval echo ~$TS_TESTUSER_DEFAULT`
	mkdir $USERHOME/IGE-testsuite
	mkdir $USERHOME/IGE-testsuite/IGE-TestSuite-Certs/
	cp -R "$TestSuiteLocation/Results" "$TestSuiteLocation/TestSuite.config" "$TestSuiteLocation/bin" "$TestSuiteLocation/software_tests" $USERHOME/IGE-testsuite
	chown -R $TS_TESTUSER_DEFAULT:users $USERHOME/IGE-testsuite
	cp -R "$TestSuiteLocation"/IGE-TestSuite-Certs/grid-security $USERHOME/IGE-testsuite/IGE-TestSuite-Certs
	su -l -p $TS_TESTUSER_DEFAULT -c "/usr/bin/grid-proxy-init"
	if [ -z $TESTTYPE ]; then
	su -l -p $TS_TESTUSER_DEFAULT -c "source $USERHOME/IGE-testsuite/TestSuite.config ; $PYBOT_CMD -i client software_tests/"
	else
		INCLUDE_STR="client&$TESTTYPE"
		su -l -p $TS_TESTUSER_DEFAULT -c "source $USERHOME/IGE-testsuite/TestSuite.config ; $PYBOT_CMD -i $INCLUDE_STR software_tests/"
	fi
	cp -R $USERHOME/IGE-testsuite/Results ~/Results
	rm -rf $USERHOME/IGE-testsuite

else 
	if grid-proxy-info &> /dev/null ; then
		if [ -z $TESTTYPE ]; then
			$PYBOT_CMD -i client software_tests/
		else
			INCLUDE_STR="client&$TESTTYPE"
			$PYBOT_CMD -i $INCLUDE_STR software_tests/
		fi
	else
		echo "We need a valid proxy to start the tests. Please use grid-proxy-init or similar to create one"
		exit 1
	fi
fi
